package liurong.driver.stream;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * 特殊字符串提取
 * 
 * @author liurong
 *
 */
public class FileStreamTest {
	private static int row1 = 1;

	public static void main(String[] args) throws Exception {
		File chinaFile = new File("D:/jack/UpdateLan.xml");
		InputStream chinaInputStream = new FileInputStream(chinaFile);
		BufferedReader chinaBufferedReader = new BufferedReader(new InputStreamReader(chinaInputStream, "iso8859-1"));

		String readLine = null;
		String enString = null;
		String enValue = null;

		// 模板路径
		String templatePath = "D:/jack/res/UpdateLan.xls";

		String createFileName = "D:/jack/res/UpdateLan_1.xls";

		String createPath = createFileName;

		FileOutputStream is = null;
		Workbook templateWB = null;
		WritableWorkbook writeWB = null;
		try {
			// 得到excel
			templateWB = Workbook.getWorkbook(new File(templatePath));

			is = new FileOutputStream(createPath);

			// 根据模板创建可写的文件
			writeWB = Workbook.createWorkbook(is, templateWB);
			// 得到第一个sheet
			WritableSheet sheet = writeWB.getSheet(0);

			// 行号
			Integer row = 1;
			while (chinaBufferedReader.ready()) {
				readLine = chinaBufferedReader.readLine();
				if (readLine.contains("en=")) {
					enString = readLine.substring(readLine.indexOf("en="));
					enValue = enString.substring(4, enString.indexOf("\" "));
					sheet.addCell(new Label(0, row, row1 + ""));
					sheet.addCell(new Label(1, row++, enValue));
				} else if (readLine.contains("lan3=\"")) {
					try {
						enString = readLine.substring(readLine.indexOf("lan3=\"") + 6);
						enValue = enString.substring(0, enString.indexOf("\" "));
						sheet.addCell(new Label(0, row, row1 + ""));
						sheet.addCell(new Label(1, row++, enValue));
					} catch (Exception e) {
						System.out.println(enString);
					}
				}
				row1++;
			}

		} catch (Exception e) {
			System.out.println(e);
		} finally {
			if (writeWB != null) {
				try {
					writeWB.write();
					writeWB.close();
					chinaBufferedReader.close();
				} catch (Exception e) {
					System.out.println(e);
				}
			}
		}
	}
}
